﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using T4Ui.dal;
using Microsoft.VisualStudio.TextTemplating.CommandLine;
using T4Ui.Utility;

namespace T4Ui
{
    public partial class GeneratorCode : Form
    {
        public GeneratorCode()
        {
            InitializeComponent();
        }

        private static log4net.ILog m_log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnGenerator_Click(object sender, EventArgs e)
        {
            try
            {
                List<Table> list = new List<Table>();
                Table tmp = null;
                foreach (DataGridViewRow drv in dgvMain.Rows)
                {
                    tmp = (Table)drv.DataBoundItem;
                    if (tmp.IsUser)
                    {
                        list.Add(tmp);
                    }
                }
                
                if (txtNameSpace.Text.Length == 0)
                {
                    MessageBox.Show("please input name space.");
                    return;
                }
                if (!(chkDal.Checked || chkModels.Checked))
                {
                    MessageBox.Show("please select generator file type.");
                    return;
                }
                if (txtFilePath.Text.Length == 0)
                {
                    MessageBox.Show("please select file path.");
                    return;
                }
                if (list.Count == 0)
                {
                    MessageBox.Show("please select one data.");
                    return;
                }                
                TableManager.TblList = list;
                TableManager.NameSpace = txtNameSpace.Text;
                TableManager.DbConn = DbHelper.dbConnectionString;
                TableManager.ParameterPre = DbHelper.ParamsterPre;
                TableManager.GenFileFolder = txtFilePath.Text;
                string ttPath ="";
                string signSqlService = "sqlServer", signOracle = "oracle";
                string objPath = AppDomain.CurrentDomain.BaseDirectory + "/tt/{0}/DTOGenerator.tt";
                string dalPath = AppDomain.CurrentDomain.BaseDirectory + "/tt/{0}/DaoGenerator.tt";
                List<string> cols = new List<string>();
                 StringBuilder sb=new StringBuilder();
                 string tmpVal = DbHelper.ProviderName.ToLower();
                 if (chkModels.Checked)
                 {
                     cols.Clear();
                     if (tmpVal == DbHelper.ProviderSqlService)
                     {
                         ttPath = string.Format(objPath, signSqlService);
                     }
                     else
                     {
                         ttPath = string.Format(objPath, signOracle);
                     }
                    // cols.Add(AppDomain.CurrentDomain.BaseDirectory + "/tt/sqlServer/DaoGenerator.tt");
                     cols.Add(ttPath);
                     TextTransform.MainText(cols.ToArray(), sb);
                 }
                 if (chkDal.Checked)               
                {
                    cols.Clear();
                    if (tmpVal == DbHelper.ProviderSqlService)
                    {
                        ttPath = string.Format(dalPath, signSqlService);
                    }
                    else
                    {
                        ttPath = string.Format(dalPath, signOracle);
                    }
                    //cols.Add(AppDomain.CurrentDomain.BaseDirectory + "/tt/sqlServer/DTOGeneratorObj.tt");
                    cols.Add(ttPath);
                    TextTransform.MainText(cols.ToArray(), sb);
                }
                MessageBox.Show(sb.ToString());

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                m_log.Error(ex);
            }
        }

        private void GeneratorCode_Load(object sender, EventArgs e)
        {
            try
            {
                dgvMain.AutoGenerateColumns = false;
                dgvMain.AllowUserToAddRows = false;
                colChkColIsUser.DataPropertyName = Table.signIsUser;
                colTxtTblName.DataPropertyName = Table.signName;
                colTxtUpdateKey.DataPropertyName = Table.signUpdateKey;
                DbBase dbDal = DbFactory.GetDbBase();
                List<Table> list=dbDal.LoadTables();
                dgvMain.DataSource = list;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                m_log.Error(ex);
            }
        }

      

        private void btnSelectFolder_Click(object sender, EventArgs e)
        {
            if (txtFilePath.Text.Length > 0)
            {
                folderBrowserDialog1.SelectedPath = txtFilePath.Text;
            }
            if (folderBrowserDialog1.ShowDialog() == DialogResult.OK)
            {
                txtFilePath.Text = folderBrowserDialog1.SelectedPath;
            }
        }

       

        private void dgvMain_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            try
            {
                if (e.ColumnIndex == 2)
                {
                    Table tbl = (Table)dgvMain.Rows[e.RowIndex].DataBoundItem;
                    ColumnFrm frm = new ColumnFrm();
                    frm.EditData = tbl;
                    StringBuilder sb = new StringBuilder();
                    if (frm.ShowDialog() == DialogResult.OK)
                    {
                        List<Column> List = frm.SelectData;
                        if (List.Count > 0)
                        {
                            sb.Append(List[0].Name);
                        }
                        for (int i = 1; i < List.Count; i++)
                        {
                            sb.Append("," + List[i].Name);
                        }
                        tbl.UpdateKey = sb.ToString();
                    }
                }
            }
            catch (Exception ex)
            {

            }
        }

        private void btnSelectAll_Click(object sender, EventArgs e)
        {
           
            foreach(DataGridViewRow drv in dgvMain.Rows)
            {
                Table tbl = (Table)drv.DataBoundItem;
                tbl.IsUser = true;
            }
            dgvMain.Refresh();
        }

        private void btnSelectNone_Click(object sender, EventArgs e)
        {
            foreach (DataGridViewRow drv in dgvMain.Rows)
            {
                Table tbl = (Table)drv.DataBoundItem;
                tbl.IsUser = false;
            }
            dgvMain.Refresh();
        }
    }
}
